package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@Slf4j
@RestController
public class UploadController {

/*
    @PostMapping("/upload") // 指定POST请求映射的路径为/upload
    public Result upload(String username, Integer age, MultipartFile image) throws Exception {
        log.info("文件上传: username:{}, age:{}, image: {}", username, age, image);
        //uuid 通用常用唯一
        String originalFilename = image.getOriginalFilename(); // 获取上传文件的原始文件名
//        int index=originalFilename.lastIndexOf(".");
//        String extname=originalFilename.substring(index);
//        String newFilename= UUID.randomUUID().toString() + extname; // 重命名上传文件名
//        log.info("新文件名: {}", newFilename);

        // 保存上传文件到指定路径
//        image.transferTo(new File("D:\\Users\\蒋果冻\\Pictures\\CSDN\\" + newFilename)); // 保存上传文件到指定路径
        image.transferTo(new File("D:\\Users\\蒋果冻\\Pictures\\CSDN\\" + originalFilename)); // 保存上传文件到指定路径
        // 在这里处理文件上传逻辑
        return Result.success();
    }*/
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传, 文件名: {}", image.getOriginalFilename());
        //调用阿里云OSS工具类进行文件上传
        String url = aliOSSUtils.upload(image);
        log.info("文件上传完成,文件访问的url: {}", url);

        return Result.success(url);
    }
}

